package kgame5.kgui.kcomp.slaverpane {
import kgame5.kutil.KEvent;

import flash.display.MovieClip;
import flash.events.EventDispatcher;

/**
 * @author kk
 * 底部托盘儿的面板,有多个按钮，鼠标放上则变大50%，鼠标离开则回复原样
 * mc_item0(mc_icon有2帧),mc_item1
 */
public class SmartSlaverPane extends EventDispatcher{
	public static const
		EVENTTYPE_CLICK_ITEM:String="EVENTTYPE_CLICK_ITEM";
	public static const DUR:Number=0.5;//动画时间
	private var mc:MovieClip;
	private var aItem:Array;//各个条目
	
	private var 
		SpaceMax:int,
		SpaceMin:int;
	
	//是否使用移动，默认是使用
	public var bUserMove:Boolean=true;
	
	public function SmartSlaverPane(mc:MovieClip,itemCount:int,SpaceMax:int,SpaceMin:int){
		
		this.SpaceMax=SpaceMax;
		this.SpaceMin=SpaceMin;
		
		this.mc=mc;
		aItem=new Array();
		for(var i:int=0;i<itemCount;i++){
			var mc_item:MovieClip=mc["mc_item"+i];
			var item:SlaverItem=new SlaverItem(i,mc_item,this);
			
			aItem.push(item);
		}
	}
	
	public function onItemClick(idx:int){
		this.dispatchEvent(new KEvent(EVENTTYPE_CLICK_ITEM,idx));
	}
	
	public function onItemRollOver(focusIdx:int){
		if(bUserMove==false){
			return;
		}
		var item:SlaverItem;
		var aimX:int;
		var i:int;
		//--
		item=aItem[focusIdx] as SlaverItem;
		var focusX:int=focusIdx*SpaceMin;
		item.moveTo(focusX);
		
		//之前的元素位置,是i的函数,var aimX:int=-(SpaceMax+(focusIdx-1-i)*SpaceMin)+focusX
		for(i=0;i<focusIdx;i++){
			aimX=focusX-(SpaceMax+(focusIdx-1-i)*SpaceMin);
			item=aItem[i] as SlaverItem;
			item.moveTo(aimX);
		}
		//之后的元素的位置,是i的函数,aimX=focusX+(SpaceMax+(i-1-focusIdx)*SpaceMin);
		for(i=focusIdx+1;i<aItem.length;i++){
			aimX=focusX+(SpaceMax+(i-1-focusIdx)*SpaceMin);
			item=aItem[i] as SlaverItem;
			item.moveTo(aimX);
		}
	}
	
	public function onItemRollOut(idx:int){
		if(bUserMove==false){
			return;
		}
		//还原距离
		var item:SlaverItem;
		var aimX:int;
		var i:int;
		
		//之前的元素位置,是i的函数,var aimX:int=-(SpaceMax+(focusIdx-1-i)*SpaceMin)+focusX
		for(i=0;i<aItem.length;i++){
			aimX=i*SpaceMin;
			item=aItem[i] as SlaverItem;
			item.moveTo(aimX);
		}
	}
}//enf of class
}
